package com.ziytek.web.citizen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ziytek.web.citizen.configuration.db.DDS;
import com.ziytek.web.citizen.constant.DdsConstant;
import com.ziytek.web.citizen.model.vo.OperationVo;
import com.ziytek.web.citizen.pojo.basic.OperationDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author fenghx
 */
@Mapper
public interface OperationDetailMapper extends BaseMapper<OperationDetail> {

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo 柜号
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByNo(String cabinetNo);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} and singleType = 0 ORDER BY createTime DESC,cellNum ")
    List<OperationDetail> findDetailNotSingle(String cabinetNo, Integer boardAddress);


    /**
     * 查询工步操作记录
     *
     * @param cabinetNo        柜号
     * @param boardAddressList 板子地址list
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("<script> " +
            "SELECT * FROM operation_detail where singleType = 0 AND cabinetNo = #{cabinetNo} AND boardAddress in  " +
            "<foreach item='boardAddress' collection='boardAddressList' open='(' separator=',' close=')'> " +
            "#{boardAddress} " +
            "</foreach> " +
            " ORDER BY createTime DESC,cellNum " +
            "</script>")

    //@Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} and singleType = 0 ORDER BY createTime DESC,cellNum ")
    List<OperationDetail> findDetailNotSingleByList(@Param("cabinetNo") String cabinetNo, @Param("boardAddressList") List<Integer> boardAddressList);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} " +
            " and singleType = 1 and startFlag in (1,2,3) ORDER BY createTime DESC,cellNum ")
    List<OperationDetail> findDetailSingleAndFinishOrInterrupt(String cabinetNo, Integer boardAddress);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo        柜号
     * @param boardAddressList 板子地址
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("<script> " +
            " SELECT * FROM operation_detail WHERE singleType = 1 AND startFlag IN (1,2,3) " +
            " AND cabinetNo = #{cabinetNo} " +
            " AND boardAddress IN  " +
            " <foreach item='boardAddress' collection='boardAddressList' open='(' separator=',' close=')'> " +
            " #{boardAddress} " +
            " </foreach> " +
            " ORDER BY createTime DESC,cellNum " +
            " </script> ")
    List<OperationDetail> findDetailSingleAndFinishOrInterruptByList(@Param("cabinetNo") String cabinetNo, @Param("boardAddressList") List<Integer> boardAddressList);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param nowTime      时间戳
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} " +
            " and singleType = 1 and endTime is not null and endTime < #{nowTime} ORDER BY createTime DESC,cellNum ")
    List<OperationDetail> findDetailSingleAndTimeout(String cabinetNo, Integer boardAddress, Long nowTime);

    /**
     * 查询工步操作记录
     *
     * @param nowTime          时间戳
     * @param cabinetNo        柜号
     * @param boardNoList 板子地址
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select(" <script> " +
            " SELECT * FROM operation_detail WHERE singleType = 1 " +
            " AND endTime IS NOT NULL AND endTime &lt; #{nowTime} " +
            " AND cabinetNo = #{cabinetNo} " +
            " AND boardAddress IN  " +
            " <foreach item='boardNo' collection='boardNoList' open='(' separator=',' close=')'> " +
            " #{boardNo} " +
            " </foreach> " +
            " ORDER BY createTime DESC,cellNum " +
            " </script> ")
    List<OperationDetail> findDetailSingleAndTimeoutByList(@Param("nowTime") Long nowTime, @Param("cabinetNo") String cabinetNo, @Param("boardNoList") List<Integer> boardNoList);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo  柜号
     * @param singleType 类型
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and singleType = #{singleType} ORDER BY createTime DESC,cellNum ")
    List<OperationDetail> findDetailNotSingleType(String cabinetNo, Integer singleType);

    /**
     * 查询工步操作记录
     *
     * @param startFlag 状态
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE startFlag = #{startFlag} ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByStartFlag(Integer startFlag);


    /**
     * 查询工步操作记录
     *
     * @param startFlag 状态
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and startFlag = #{startFlag} ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByCabinetNoAndStartFlag(String cabinetNo, Integer startFlag);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo cabinetNo
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT DISTINCT(boardAddress) as boardNo FROM operation_detail WHERE cabinetNo = 0 ORDER BY createTime DESC ")
    List<Integer> findBoardNosByCabinetNo(String cabinetNo);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo cabinetNo
     * @param boardNo   boardNo
     * @param startFlag 状态
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
//    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardNo} " +
//            "and startFlag = #{startFlag}  and endTime is not null and endTime >= #{nowTime} ORDER BY createTime DESC ")
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardNo} " +
            "and startFlag = #{startFlag} ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByCabinetNoAndBoardNoAndStartFlag(String cabinetNo, Integer boardNo, Integer startFlag, Long nowTime);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo cabinetNo
     * @param boardNo   boardNo
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
//    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardNo} " +
//            "  and endTime is not null and endTime >= #{nowTime} ORDER BY createTime DESC ")
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardNo} " +
            " ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByCabinetNoAndBoardNo(String cabinetNo, Integer boardNo, Long nowTime);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo cabinetNo
     * @return List<OperationDetailTbl>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} ORDER BY createTime DESC ")
    List<OperationDetail> findDetailByCabinetNo(String cabinetNo);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @param boardLineNum 行号
     * @return OperationDetailTbl
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} AND cellNum = #{cellNum} AND boardLineNum = #{boardLineNum}  ORDER BY createTime DESC,cellNum  LIMIT 1")
    OperationDetail findOneDetail(String cabinetNo, Integer boardAddress, Integer cellNum, Integer boardLineNum);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @return OperationDetailTbl
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} AND cellNum = #{cellNum} ORDER BY createTime DESC,cellNum  LIMIT 1")
    OperationDetail findOneDetailByCellNum(String cabinetNo, Integer boardAddress, Integer cellNum);


    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @return OperationDetailTbl
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress} AND cellNum = #{cellNum} AND singleType = #{type} ORDER BY createTime DESC,cellNum  LIMIT 1")
    OperationDetail findOneDetailByCellNumAndType(String cabinetNo, Integer boardAddress, Integer cellNum, Integer type);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @return OperationDetailTbl
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress}" + " AND cellNum = #{cellNum} AND stepId = #{stepId} ORDER BY firstOrderTime DESC LIMIT 1")
    OperationDetail findOneDetailByOrderNo(String cabinetNo, Integer boardAddress, Integer cellNum, Integer stepId);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @return OperationDetailTbl
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT * FROM operation_detail WHERE cabinetNo = #{cabinetNo} and boardAddress = #{boardAddress}" + " AND cellNum = #{cellNum} ORDER BY firstOrderTime DESC LIMIT 1")
    OperationDetail findOneDetailLast(String cabinetNo, Integer boardAddress, Integer cellNum);

    @DDS(DdsConstant.DS_BASIC)
    @Insert({"insert into operation_detail(endTime,cabinetNo, batterySerial, stepId, startFlag, operationId, singleType,"
            + " cellNum, orderNo, boardLineNum, boardAddress, createTime, updateTime,firstOrderTime," +
            " interruptTimeSecond,interruptCap,beginTime,batchNo) values "
            + " (#{endTime},#{cabinetNo}, #{batterySerial}, #{stepId}, #{startFlag}, #{operationId}, #{singleType}, "
            + "#{cellNum}, #{orderNo}, #{boardLineNum}, #{boardAddress},#{createTime},#{updateTime},#{firstOrderTime}" +
            " ,#{interruptTimeSecond}, #{interruptCap},#{beginTime},#{batchNo})"})
    int addDetail(OperationDetail operationDetail);

    @DDS(DdsConstant.DS_BASIC)
    @Update({"update operation_detail set batterySerial = #{batterySerial},"
            + " stepId = #{stepId}, "
            + " batchNo = #{batchNo}, "
            + " beginTime = #{beginTime}, "
            + " interruptTimeSecond = #{interruptTimeSecond}, "
            + " interruptCap = #{interruptCap}, "
            + " endTime = #{endTime}, "
            + " startFlag = #{startFlag}, "
            + " operationId = #{operationId}, "
            + " singleType = #{singleType}, "
            + " cellNum = #{cellNum}, orderNo = #{orderNo}, boardLineNum = #{boardLineNum}, "
            + " boardAddress = #{boardAddress}, updateTime = #{updateTime} ,firstOrderTime = #{firstOrderTime} "
            + " where id = #{id}"})
    int updateDetail(OperationDetail operationDetail);

    @DDS(DdsConstant.DS_BASIC)
    @Delete({"delete from operation_detail where id = #{id}"})
    int deleteDetail(OperationDetail operationDetail);

    @DDS(DdsConstant.DS_BASIC)
    @Delete({"delete from operation_detail where id is not null and cabinetNo = #{cabinetNo} and singleType=0 "})
    int deleteDetailByCabinetNo(String cabinetNo);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo    柜号
     * @param boardAddress 板子地址
     * @param cellNum      电芯号
     * @return OperationVo
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT o.endTime,o.stepId,s.stepId as detailsStepId,o.startFlag,o.cellNum,"
            + " o.operationId,o.boardLineNum,o.boardAddress,"
            + " o.cabinetNo,o.orderNo,"
            + " o.singleType,s.settingOrder,s.setType,"
            + " s.cutOff,s.cutOffType,s.current ,s.expectedTime, o.interruptTimeSecond, o.interruptCap, o.batchNo " + " FROM operation_detail o "
            + " LEFT JOIN setting_detail s " + " ON o.stepId = s.interruptOrEndStepId AND o.orderNo = s.settingOrder "
            + " WHERE s.isDelete = 0 AND s.interruptOrEndStepId > 0 "
            + " AND o.cabinetNo = #{cabinetNo}  AND o.cellNum = #{cellNum}  "
            + " AND o.boardAddress = #{boardAddress} ORDER BY o.firstOrderTime DESC limit 1 ")
    OperationVo findLeftOn(@Param("cabinetNo") String cabinetNo, @Param("cellNum") Integer cellNum, @Param("boardAddress") Integer boardAddress);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo 柜号
     * @return List<OperationVo>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT o.endTime,o.stepId,s.stepId as detailsStepId,o.startFlag,o.cellNum,"
            + " o.operationId,o.boardLineNum,o.boardAddress,"
            + " o.cabinetNo,o.orderNo,"
            + " o.singleType,s.settingOrder,s.setType,"
            + " s.cutOff,s.cutOffType,s.current ,s.expectedTime, o.interruptTimeSecond, o.interruptCap, o.batchNo " + " FROM operation_detail o "
            + " LEFT JOIN setting_detail s " + " ON o.stepId = s.interruptOrEndStepId AND o.orderNo = s.settingOrder "
            + " WHERE s.isDelete = 0 AND s.interruptOrEndStepId > 0 "
            + " AND o.cabinetNo = #{cabinetNo} AND o.orderNo = #{orderNo} ORDER BY o.firstOrderTime DESC ")
    List<OperationVo> findListLeftOn(@Param("cabinetNo") String cabinetNo, @Param("orderNo") Integer orderNo);

    /**
     * 查询工步操作记录
     *
     * @param cabinetNo 柜号
     * @return List<OperationVo>
     */
    @DDS(DdsConstant.DS_BASIC)
    @Select("SELECT o.endTime,o.stepId,s.stepId as detailsStepId,o.startFlag,o.cellNum,"
            + " o.operationId,o.boardLineNum,o.boardAddress,"
            + " o.cabinetNo,o.orderNo,"
            + " o.singleType,s.settingOrder,s.setType,"
            + " s.cutOff,s.cutOffType,s.current ,s.expectedTime, o.interruptTimeSecond, o.interruptCap, o.batchNo " + " FROM operation_detail o "
            + " LEFT JOIN setting_detail s " + " ON o.stepId = s.interruptOrEndStepId AND o.orderNo = s.settingOrder "
            + " WHERE s.isDelete = 0 AND s.interruptOrEndStepId > 0 "
            + " AND o.cabinetNo = #{cabinetNo} AND o.singleType = 0 ORDER BY o.firstOrderTime DESC ")
    List<OperationVo> findListLeftOnByList(@Param("cabinetNo") String cabinetNo);
}
